package com.ruicar.afs.cloud.apply.pre.loan.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.apply.common.entity.*;
import com.ruicar.afs.cloud.apply.pre.loan.condition.ApplyAppFormStateCondition;
import com.ruicar.afs.cloud.apply.pre.loan.service.*;
import com.ruicar.afs.cloud.bizcommon.business.entity.FinCostDetails;
import com.ruicar.afs.cloud.bizcommon.business.service.ApplyCostDetailsService;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.image.entity.ComAttachmentFile;
import com.ruicar.afs.cloud.parameter.commom.enums.FormalReviewEnums;
import com.ruicar.afs.cloud.parameter.commom.enums.OrderOprType;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.ruicar.afs.cloud.apply.common.utils.ApplyConstants.*;

/**
 * @description: app表单状态(进件)
 * @author: mingzhi.li
 * @date: 2020/8/03 14:28
 */
@Slf4j
@RestController
@RequestMapping("/applyAppFormState")
@AllArgsConstructor
public class ApplyAppFormStateController {
    /**
     * 订单申请
     */
    private final ApplyOrderInfoService applyOrderInfoService;
    private final ApplyCostDetailsService costDetailsService;
    private final ApplyAppService applyAppService;
    private final ApplyOprRecordService applyOprRecordService;
    private final ApplyCustBaseInfoService applyCustBaseInfoService;

    @RequestMapping(value = "/getApplyAppFormState", method = RequestMethod.POST)
    @ApiOperation(value = "根据申请编号获取app表单是否已经录入")
    public IResponse<Object>  getApplyAppFormState(@RequestBody ApplyAppFormStateCondition stateCondition) {
        if(StrUtil.isBlank(stateCondition.getApplyNo())){
            return IResponse.fail("申请编号不能为空");
        }
        ApplyAppFormStateCondition condition = null;
        ApplyOrderInfo applyOrderInfo = applyOrderInfoService.getOne(Wrappers.<ApplyOrderInfo>query().lambda().eq(ApplyOrderInfo::getApplyNo, stateCondition.getApplyNo()));
        log.info("======================业务类型区域组装开始========================》");
        condition = applyAppService.getBusinessTypeArea(stateCondition);
        log.info("======================业务类型区域组装结束========================》");
        condition.setApplyStatus(applyOrderInfo.getApplyStatus());
        if(applyOrderInfo.getPreId() !=null){
            condition.setPreFlag(true);
        }else{
            condition.setPreFlag(stateCondition.isPreFlag());
        }
        condition.setCarType(applyOrderInfo.getCarType());
        log.info("======================挂靠信息区域组装开始========================》");
        condition = applyAppService.getAffiliatedInfoArea(stateCondition,applyOrderInfo);
        log.info("======================挂靠信息区域组装结束========================》");


        log.info("======================资产信息区域组装开始========================》");
        condition = applyAppService.getAssetsInfoArea(stateCondition,applyOrderInfo);
        log.info("======================资产信息区域组装结束========================》");

        log.info("======================客户信息区域组装开始========================》");
        condition = applyAppService.getCustInfoArea(stateCondition,applyOrderInfo);
        log.info("======================客户信息区域组装结束========================》");

        log.info("======================金融产品区域组装开始========================》");
        List<FinCostDetails> resultList = costDetailsService.list(Wrappers.<FinCostDetails>query().lambda()
                .eq(FinCostDetails::getApplyNo, stateCondition.getApplyNo()));
        if(resultList!=null && resultList.size()>0){
            condition.setFinancialProductsState(APP_FORM_STATE_ONE);
        }else{
            condition.setFinancialProductsState(APP_FORM_STATE_ZERO);
        }
        log.info("======================金融产品区域组装结束========================》");


        log.info("======================签约协议区域组装开始========================》");
        condition = applyAppService.getSignInfoArea(condition);
        log.info("======================签约协议区域组装结束========================》");

        log.info("======================影像附件区域组装开始========================》");
        stateCondition.setBusiNo(condition.getApplyNo());
        stateCondition.setBusiType("orderApply");
        condition = applyAppService.getAttachmentsArea(stateCondition,applyOrderInfo);
        log.info("======================影像附件区域组装结束========================》");
        log.info("======================是否已生成合同开始========================》");
        condition.setIsApplyContract(applyAppService.getApplyFileIsComplete(condition));
        log.info("======================是否已生成合同结束========================》");

        log.info("======================是否处于复议流程中========================》");
        ApplyOprRecord applyOprRecord = applyOprRecordService.getOne(Wrappers.<ApplyOprRecord>query().lambda()
                .eq(ApplyOprRecord::getApplyNo, condition.getApplyNo())
                .eq(ApplyOprRecord::getApplyType, OrderOprType.RECONSIDER)
                .eq(ApplyOprRecord::getApplyStatus, FormalReviewEnums.REFUSALREVIEW.getCode()));
        if(applyOprRecord !=null){
            condition.setIsReconsider(APP_FORM_STATE_ONE);
        }else{
            condition.setIsReconsider(APP_FORM_STATE_ZERO);
        }
        log.info("======================是否处于复议流程中========================》");

        log.info("======================判断是否需要重出进件合同开始========================》");
        List<ApplyCustBaseInfo> infoList = applyCustBaseInfoService.list(Wrappers.<ApplyCustBaseInfo>query().lambda()
                .eq(ApplyCustBaseInfo::getApplyNo, condition.getApplyNo())
                .eq(ApplyCustBaseInfo::getReappearFlag,YES));
        if(YES.equals(applyOrderInfo.getReappearFlag())|| (infoList !=null && infoList.size()>0)){
            condition.setReappearFlag(YES);
        }else{
            condition.setReappearFlag(NO);
        }
        log.info("======================判断是否需要重出进件合同结束========================》");
        return  IResponse.success(condition);
    }
}
